package com.example.kliven.because.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.kliven.because.R;
import com.example.kliven.because.bean.ContactBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

/**
 * Created by Kliven on 16/1/13.
 */
public class ContactAdapter extends BaseAdapter{
    private ArrayList<ContactBean> contactBeans;
    private Context context;

    private HashMap<String,Integer> alphoaIndexer;// 字母检索;
    private String[] sections; // 存储每个章节

    public ContactAdapter(ArrayList<ContactBean> contactBeans, Context context) {
        this.contactBeans = contactBeans;
        this.context = context;

    }

    @Override
    public int getCount() {
        return contactBeans.size() > 0 && contactBeans!= null ? contactBeans.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return contactBeans.size() > 0 && contactBeans!= null ? contactBeans.get(i) : 0;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }



    public View getView(int postion, View view, ViewGroup viewGroup) {

        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_contact,null);
        }
        ImageView headimage = (ImageView)view.findViewById(R.id.fragment_free_iv);
        TextView phonename = (TextView)view.findViewById(R.id.fragment_free_tv_name);
        TextView phonenumber = (TextView)view.findViewById(R.id.fragment_free_tv_number);


        ContactBean bean = contactBeans.get(postion);
        headimage.setImageBitmap(bean.getHeadimage());
        phonename.setText(bean.getPnonename());
        phonenumber.setText(bean.getPnonenumber());


        return view;
    }


    private String getAlpha(String str){
        if (str == null){
            return "#";
        }
        if (str.trim().length() == 0){
            return "#";
        }
        char c = str.trim().substring(0,1).charAt(0);
        // 正则表达式匹配;
        Pattern pattern = Pattern.compile("[A-Za-z]+$");
        if (pattern.matcher(c + "").matches()){
            return (c + "").toUpperCase();//将小字母转换为大写;
        }else{
            return "#";
        }
    }
}
